package by.epam.trenings.vkphotorest.content;

import by.epam.trenings.vkphotorest.content.contract.TaskContract;
import by.epam.trenings.vkphotorest.content.database.AlbumsTable;
import by.epam.trenings.vkphotorest.content.database.PhotosTable;
import by.epam.trenings.vkphotorest.content.database.common.AlbumDatabaseHelper;
import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.net.Uri;

public class ApplicationProvider extends ContentProvider {

	private static final UriMatcher sUriMatcher;

	/** The constant for UriMatcher */
	private static final int URI_PHOTOS = 1;

	/** The constant for UriMatcher */
	private static final int URI_PHOTOS_ID = 2;

	/** The constant for UriMatcher */
	private static final int URI_ALBUMS = 3;

	/** The constant for UriMatcher */
	private static final int URI_ALBUMS_ID = 4;

	public static final String EXTRA_DATA = "data";

	public static AlbumDatabaseHelper mPhotoDatabase;

	@Override
	public boolean onCreate() {
		mPhotoDatabase = new AlbumDatabaseHelper(getContext(), null);
		return true;
	}

	static {
		sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);

		sUriMatcher.addURI(TaskContract.AUTHORITY, "photos", URI_PHOTOS);
		sUriMatcher.addURI(TaskContract.AUTHORITY, "photos" + "/*",
				URI_PHOTOS_ID);

		sUriMatcher.addURI(TaskContract.AUTHORITY, "albums", URI_ALBUMS);
		sUriMatcher.addURI(TaskContract.AUTHORITY, "albums" + "/*",
				URI_ALBUMS_ID);
	}

	@Override
	public int delete(final Uri uri, final String selection,
			final String[] selectionArgs) {
		int count;
		if (sUriMatcher.match(uri) == URI_PHOTOS_ID) {
			final String mySelection = selection + "="
					+ uri.getLastPathSegment();
			count = mPhotoDatabase.deleteTasks(mySelection, selectionArgs,
					PhotosTable.PHOTOS_TABLE);
		} else if (sUriMatcher.match(uri) == URI_PHOTOS) {
			count = mPhotoDatabase.deleteTasks(selection, selectionArgs,
					PhotosTable.PHOTOS_TABLE);
		} else if (sUriMatcher.match(uri) == URI_ALBUMS) {
			count = mPhotoDatabase.deleteTasks(selection, selectionArgs,
					AlbumsTable.ALBUMS_TABLE);
		} else if (sUriMatcher.match(uri) == URI_ALBUMS_ID) {
			final String mySelection = selection + "="
					+ uri.getLastPathSegment();
			count = mPhotoDatabase.deleteTasks(mySelection, selectionArgs,
					AlbumsTable.ALBUMS_TABLE);
		} else {
			throw new IllegalArgumentException("Unknown URI " + uri);
		}
		getContext().getContentResolver().notifyChange(uri, null);
		return count;
	}

	@Override
	public String getType(final Uri uri) {
		String type = null;
		if (sUriMatcher.match(uri) == URI_PHOTOS) {
			type = TaskContract.PhotosColumns.CONTENT_TYPE;
		} else if (sUriMatcher.match(uri) == URI_ALBUMS) {
			type = TaskContract.AlbumsColumns.CONTENT_TYPE;
		} else {
			throw new IllegalArgumentException("Unknown URI " + uri);
		}
		return type;
	}

	@Override
	public Uri insert(final Uri uri, final ContentValues values) {
		String jsonData = values.getAsString(EXTRA_DATA);
		Uri taskUri;
		if (sUriMatcher.match(uri) == URI_PHOTOS) {
			long id = mPhotoDatabase.addTasks(
					PhotosTable.getContentValues(jsonData),
					PhotosTable.PHOTOS_TABLE);
			taskUri = ContentUris.withAppendedId(
					TaskContract.PhotosColumns.CONTENT_URI, id);
		} else if (sUriMatcher.match(uri) == URI_ALBUMS) {
			long id = mPhotoDatabase.addTasks(
					AlbumsTable.getContentValues(jsonData),
					AlbumsTable.ALBUMS_TABLE);
			taskUri = ContentUris.withAppendedId(
					TaskContract.AlbumsColumns.CONTENT_URI, id);
		} else {
			throw new IllegalArgumentException("Unknown URI " + uri);
		}
		getContext().getContentResolver().notifyChange(taskUri, null);
		return taskUri;
	}

	@Override
	public Cursor query(final Uri uri, final String[] columns,
			final String selection, final String[] selectionArgs,
			final String sortOrder) {
		Cursor data = null;
		if (sUriMatcher.match(uri) == URI_PHOTOS_ID) {

			final String mySelection = selection + "="
					+ uri.getLastPathSegment();
			data = mPhotoDatabase.getTasks(columns, mySelection, selectionArgs,
					null, null, sortOrder, PhotosTable.PHOTOS_TABLE);

		} else if (sUriMatcher.match(uri) == URI_PHOTOS) {
			data = mPhotoDatabase.getTasks(columns, selection, selectionArgs,
					null, null, sortOrder, PhotosTable.PHOTOS_TABLE);
		} else if (sUriMatcher.match(uri) == URI_ALBUMS) {

			data = mPhotoDatabase.getTasks(columns, selection, selectionArgs,
					null, null, sortOrder, AlbumsTable.ALBUMS_TABLE);

		} else if (sUriMatcher.match(uri) == URI_ALBUMS_ID) {
			final String mySelection = selection + "="
					+ uri.getLastPathSegment();
			data = mPhotoDatabase.getTasks(columns, mySelection, selectionArgs,
					null, null, sortOrder, AlbumsTable.ALBUMS_TABLE);
		} else {
			throw new IllegalArgumentException("Unknown URI " + uri);
		}

		data.setNotificationUri(getContext().getContentResolver(), uri);
		return data;
	}

	@Override
	public int update(final Uri uri, final ContentValues values,
			final String selection, final String[] selectionArgs) {
		int count;
		String jsonData = values.getAsString(EXTRA_DATA);

		if (sUriMatcher.match(uri) == URI_PHOTOS_ID) {
			final String mySelection = selection + "="
					+ uri.getLastPathSegment();
			count = mPhotoDatabase.updateTasks(
					PhotosTable.getContentValues(jsonData), mySelection,
					selectionArgs, PhotosTable.PHOTOS_TABLE);
		} else if (sUriMatcher.match(uri) == URI_PHOTOS) {
			count = mPhotoDatabase.updateTasks(
					PhotosTable.getContentValues(jsonData), selection,
					selectionArgs, PhotosTable.PHOTOS_TABLE);
		} else if (sUriMatcher.match(uri) == URI_ALBUMS) {
			count = mPhotoDatabase.updateTasks(
					AlbumsTable.getContentValues(jsonData), selection,
					selectionArgs, AlbumsTable.ALBUMS_TABLE);

		} else if (sUriMatcher.match(uri) == URI_ALBUMS_ID) {
			final String mySelection = selection + "="
					+ uri.getLastPathSegment();
			count = mPhotoDatabase.updateTasks(
					AlbumsTable.getContentValues(jsonData), mySelection,
					selectionArgs, AlbumsTable.ALBUMS_TABLE);
		} else {
			throw new IllegalArgumentException("Unknown URI " + uri);
		}
		getContext().getContentResolver().notifyChange(uri, null);
		return count;
	}
}
